//
//  ConversationCommentCell.m
//  LookScore
//
//  Created by wws on 2018/1/24.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import <UIImageView+WebCache.h>
#import "ConversationCommentCell.h"
#import "LSSecondCommentCell.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"
#import "ConversationIssueModel.h"

@interface ConversationCommentCell()<UITableViewDelegate,UITableViewDataSource>{
  
    //用户头像
    UIImageView *_heraderImageView;
    //评论内容
    UILabel *_commentLable;
    //用户昵称
    UILabel *_nickNameLable;
    //点赞按钮
    UIButton *_praiseButton;
    //评论时间
    UILabel *_commentTimeLable;
    
    UIButton *_commentCountBtn;
    
    UIView * _bgView;
    
    UITableView * _tableView;
    
    UIButton * _lookBtn;//查看更多按钮
    
    UIView * _lineView1;//底部的线
    
    UIButton * _hostBtn;//楼主
    
    NSInteger _pageCount;
    
   
    
}

@property (nonatomic, strong) NSMutableArray * dataArr;
//@property (nonatomic, strong) NSMutableArray * commentArray;
@end

@implementation ConversationCommentCell{
    
     CGFloat tableViewHeight;
}

//- (NSMutableArray *)dataArr{
//    if (!_dataArr) {
//        _dataArr = @[].mutableCopy;
//    }
//    return _dataArr;
//}

//- (NSMutableArray *)commentArray{
//    if (!_commentArray) {
//        _commentArray = @[].mutableCopy;
//    }
//
//    return _commentArray;
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(ConversationCommentCell *)ConversationCommentCellWidthTableView:(UITableView *)tableView{
    
    ConversationCommentCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[ConversationCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _pageCount = 1;
        [self addCommentIssueSubViews];
    }
    
    return self;
}


/*评论内容详情*/
-(void)addCommentIssueSubViews{
    
    
   
    //加入用户头像
    UIImageView *heraderImageView=[UIImageView new];
    _heraderImageView=heraderImageView;
    heraderImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageView)];
    [heraderImageView addGestureRecognizer:tap];
    //用户昵称
    UILabel *nickNameLable=[UILabel new];
    nickNameLable.textColor=LSTitleGrayColor1;
    nickNameLable.font=LSFontSize15;
    _nickNameLable=nickNameLable;
    
    
    //话题内容
    UILabel *commentLable=[UILabel new];
    commentLable.numberOfLines=0;
    commentLable.textColor=[[UIColor blackColor] colorWithAlphaComponent:0.7];;
    commentLable.font=LSFontSize14;
    _commentLable=commentLable;
    
    //评论时间
    UILabel *commentTimeLable=[[UILabel alloc]init];
    commentTimeLable.font=LSFontSize10;
    commentTimeLable.textColor=LSTitleGrayColor3;
    commentTimeLable.textAlignment=NSTextAlignmentLeft;
    _commentTimeLable=commentTimeLable;
    
    //楼主
    UIButton * hostBtn = [UIButton new];
    hostBtn.layer.cornerRadius = 5;
    hostBtn.layer.borderWidth = 1;
    hostBtn.layer.borderColor = LSSubColor9dd3cc.CGColor;
    hostBtn.titleLabel.font = LSFontSize9;
    [hostBtn setTitle:@"楼主" forState:UIControlStateNormal];
    [hostBtn setTitleColor:LSSubColor9dd3cc forState:UIControlStateNormal];
    _hostBtn = hostBtn;
    hostBtn.hidden = YES;

    
    //点赞按钮
    UIButton *praiseButton=[UIButton new];
    [praiseButton setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [praiseButton setImage:[UIImage imageNamed:@"ChatPraise"] forState:UIControlStateNormal];
    praiseButton.titleLabel.font=LSFontSize10;
    [praiseButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -10)];
    [praiseButton addTarget:self action:@selector(praiseButtonSender:) forControlEvents:UIControlEventTouchUpInside];
    _praiseButton=praiseButton;
    
    
    UIButton * commentBtn = [UIButton new];
    [commentBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [commentBtn setImage:[UIImage imageNamed:@"ChatComment"] forState:UIControlStateNormal];
    commentBtn.titleLabel.font = LSFontSize10;
    [commentBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -10)];
    _commentCountBtn = commentBtn;
    
    
//    MenuView * operationView = [[MenuView alloc] init];ADZ  
//    _operationView = operationView;
    
//    KWeakSelf(self);
//    [operationView setLikeBtnOnClicked:^{
//        if ([weakself.delegate respondsToSelector:@selector(reportActionWithCell:)]) {
//            [weakself.delegate reportActionWithCell:weakself];
//        }
//    }];
//    [operationView setCommentBtnOnClicked:^{
//        if ([weakself.delegate respondsToSelector:@selector(commentActionWithCell:)]) {
//            [weakself.delegate commentActionWithCell:weakself];
//        }
//    }];
//
//    //sd布局
    [self.contentView sd_addSubviews:@[heraderImageView,nickNameLable,commentLable,commentTimeLable,praiseButton,commentBtn,hostBtn]];
    
    heraderImageView.sd_layout
    .widthIs(30)
    .heightEqualToWidth()
    .leftSpaceToView(self.contentView,LSSpaceLeftWidth1)
    .topSpaceToView(self.contentView, 12);
    
    praiseButton.sd_layout
    .widthIs(50)
    .heightIs(20)
    .centerYEqualToView(heraderImageView)
    .rightSpaceToView(self.contentView, LSSpaceLeftWidth1);
    
    nickNameLable.sd_layout
    .leftSpaceToView(heraderImageView, 5)
    .centerYEqualToView(heraderImageView)
    .heightIs(20);
    [nickNameLable setSingleLineAutoResizeWithMaxWidth:180];
    
    commentLable.sd_layout
    .topSpaceToView(heraderImageView,15)
    .leftEqualToView(heraderImageView)
    .rightEqualToView(praiseButton)
    .autoHeightRatio(0);
    
    commentTimeLable.sd_layout
    .leftEqualToView(nickNameLable)
    .heightIs(20)
    .topSpaceToView(commentLable, 5);
    [commentTimeLable setSingleLineAutoResizeWithMaxWidth:180];
    
    commentBtn.sd_layout
    .rightSpaceToView(self.contentView, LSSpaceLeftWidth1)
    .centerYEqualToView(commentTimeLable)
    .widthIs(50)
    .heightIs(20);
    
    hostBtn.sd_layout
    .leftSpaceToView(nickNameLable, 4)
    .centerYEqualToView(nickNameLable)
    .heightIs(15)
    .widthIs(25);
    
//    operationView.sd_layout
//    .rightSpaceToView(commentBtn, 5)
//    .centerYEqualToView(commentBtn)
//    .heightIs(36)
//    .widthIs(0);
    
    
    //背景
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = LSBackGroundColor;
    [self.contentView addSubview:bgView];
    _bgView = bgView;
    bgView.sd_layout
    .leftEqualToView(_nickNameLable)
    .rightEqualToView(commentBtn).heightIs(40)
    .topSpaceToView(commentTimeLable, 5);
    
    //竖线
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = LSTitleGrayColor3;
    [bgView addSubview:lineView];
    lineView.sd_layout
    .leftSpaceToView(bgView, 5)
    .topSpaceToView(bgView, 10)
    .bottomSpaceToView(bgView, 10)
    .widthIs(2);
    
    //相关评论
    UILabel * xgplLbale = [[UILabel alloc] init];
    xgplLbale.textColor = LSTitleGrayColor3;
    xgplLbale.text = @"相关评论";
    xgplLbale.font = LSFontSize14;
    [bgView addSubview:xgplLbale];
    xgplLbale.sd_layout
    .leftSpaceToView(lineView, 5)
    .centerYEqualToView(lineView)
    .heightIs(20);
    [xgplLbale setSingleLineAutoResizeWithMaxWidth:180];
    
    //嵌套的tableview
    UITableView * tableView = [[UITableView alloc] init];
    tableView.scrollEnabled = NO;
    [tableView registerClass:[LSSecondCommentCell class] forCellReuseIdentifier:@"LSSecondCommentCell"];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView = tableView;
    [self.contentView addSubview:tableView];
    tableView.sd_layout
    .leftEqualToView(_nickNameLable)
    .topSpaceToView(bgView, 0)
    .rightEqualToView(commentBtn);
    
    //查看更多评论按钮
    UIButton * lookBtn = [UIButton new];
    [lookBtn setTitle:@"查看剩余评论" forState:UIControlStateNormal];
    lookBtn.titleLabel.font = LSFontSize14;
    [lookBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [lookBtn addTarget:self action:@selector(seeMoreComments:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:lookBtn];
    _lookBtn = lookBtn;
    lookBtn.sd_layout
    .leftEqualToView(commentTimeLable).offset(10)
    .topSpaceToView(tableView, 3)
    .heightIs(30)
    .widthIs(110);
    
    //底部的线条
    UIView * lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = LSLineGrayColor;
    _lineView1 = lineView1;
    [self.contentView addSubview: lineView1];
    lineView1.sd_layout
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .topSpaceToView(lookBtn, 15)
    .heightIs(1);

    [self setupAutoHeightWithBottomView:lineView1 bottomMargin:0.0];
    
    self.hyb_lastViewInCell = lineView1;
    self.hyb_bottomOffsetToCell = 0.0;//cell底部距离为3.0个间隙
    
}


/*点赞按钮*/
-(void)praiseButtonSender:(UIButton *)sender{
    
    sender.selected=!sender.selected;
    
    if ([_delegate respondsToSelector:@selector(thumbupActionWithIndexPath:)]) {
        [_delegate thumbupActionWithIndexPath:self.myIndexPath];
    }
    
}

- (void)setCommentArray:(NSMutableArray *)commentArray{
    
    _commentArray = commentArray;
   
    
}

/*模型赋值*/
-(void)setCommentModel:(CommentIssueModel *)commentModel{

    _commentModel = commentModel;
    
    //用户头像
    [_heraderImageView sd_setImageWithURL:[NSURL URLWithString:commentModel.UserHeadUrl] placeholderImage:[UIImage imageNamed:@"MineDefaultHead"] options:SDWebImageRefreshCached];
    //用户昵称
    _nickNameLable.text = commentModel.UserDisName;
    //评论内容
    _commentLable.text = commentModel.MainContent;
    //评论时间
    _commentTimeLable.text = [commentModel.CreateTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    //浏览人数
    [_praiseButton setTitle:[NSString stringWithFormat:@"%ld",commentModel.ThumbupCount] forState:UIControlStateNormal];
    [_commentCountBtn setTitle:[NSString stringWithFormat:@"%ld",commentModel.CommentCount] forState:UIControlStateNormal];
    if (commentModel.SecondCommentList.count <= 0) {
        _bgView.hidden = YES;
        _lookBtn.hidden = YES;
        
        _lineView1.sd_resetLayout
        .leftEqualToView(self.contentView)
        .rightEqualToView(self.contentView)
        .topSpaceToView(_commentTimeLable, 15)
        .heightIs(1);
        
    }else{
        if (commentModel.SecondCommentList.count < 3) {
            _lookBtn.hidden = YES;
        }else{
            _lookBtn.hidden = NO;
            
        }
        _bgView.hidden = NO;
        _lineView1.sd_resetLayout.leftEqualToView(self.contentView)
        .rightEqualToView(self.contentView)
        .topSpaceToView(_lookBtn, 15)
        .heightIs(1);
    }
    if (commentModel.isExpand) {
        _lookBtn.hidden = YES;
    }
    if (self.commentArray == nil) {
        self.commentArray = @[].mutableCopy;
    }
    tableViewHeight = 0;
    NSInteger count = 0;
    if (commentModel.pageCount >= commentModel.SecondCommentList.count) {
        count = commentModel.SecondCommentList.count;
    }else{
        count = commentModel.pageCount;
    }
    for (NSInteger i = 0; i<count; i++) {
        id obj = commentModel.SecondCommentList[i];
        if ([obj isKindOfClass:[NSArray class]]) {

        }else{
    
            LSSecondCommentModel * model= (LSSecondCommentModel *)obj;
            CGFloat cellH = [LSSecondCommentCell hyb_heightForTableView:_tableView config:^(UITableViewCell *sourceCell) {
                LSSecondCommentCell * cell = (LSSecondCommentCell *)sourceCell;
                cell.model = model;
            } cache:^NSDictionary *{
                return @{kHYBCacheUniqueKey : @(model.Id),
                         kHYBCacheStateKey : @"",
                         kHYBRecalculateForStateKey : @(YES)};
            }];
            tableViewHeight += cellH;
        }
    }
    _tableView.sd_resetLayout
    .leftEqualToView(_nickNameLable)
    .topSpaceToView(_bgView, 0)
    .rightEqualToView(_commentCountBtn)
    .heightIs(tableViewHeight);
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView reloadData];
    
}

- (void)setConverModel:(ConversationIssueModel *)converModel{
    _converModel = converModel;
    if ([converModel.UserCid isEqualToString:_commentModel.UserCid]) {
        _hostBtn.hidden = NO;
    }else{
        
        _hostBtn.hidden = YES;
    }
    
    
}


#pragma mark -- UITableViewDelegate & dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSSecondCommentCell * cell = [LSSecondCommentCell LSSecondCommentCellCellWidthTableView:tableView];
    cell.firstModel = self.commentModel;
    cell.model = self.commentModel.SecondCommentList[indexPath.row];
//    cell.model = self.dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.commentModel.pageCount <= self.commentModel.SecondCommentList.count) {
       return self.commentModel.pageCount;
    }else{
        return self.commentModel.SecondCommentList.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    LSSecondCommentModel *model = self.commentModel.SecondCommentList[indexPath.row];
    CGFloat cell_height = [LSSecondCommentCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        LSSecondCommentCell *cell = (LSSecondCommentCell *)sourceCell;
        cell.model = model;
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : @(model.Id),
                                kHYBCacheStateKey : @"",
                                kHYBRecalculateForStateKey : @(NO)};
        // model.shouldUpdateCache = NO;
        return cache;
    }];
    return cell_height;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LSSecondCommentModel * model = self.commentModel.SecondCommentList[indexPath.row];
//     LSSecondCommentModel *model = self.dataArr[indexPath.row];
    CGFloat cellH = [LSSecondCommentCell hyb_heightForTableView:_tableView config:^(UITableViewCell *sourceCell) {
        LSSecondCommentCell * cell = (LSSecondCommentCell * )sourceCell;
        cell.model = model;
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : @(model.Id),
                                kHYBCacheStateKey : @"",
                                kHYBRecalculateForStateKey : @(NO)};
        //        model.shouldUpdateCache = NO;
        return cache;
    }];
    if ([_delegate respondsToSelector:@selector(passCellHeight:secondCommentModel:commentCell:withConversationCommentCell:)]) {
        LSSecondCommentCell * cell = (LSSecondCommentCell *)[tableView cellForRowAtIndexPath:indexPath];
        [_delegate passCellHeight:cellH secondCommentModel:model commentCell:cell withConversationCommentCell:self];
    }
}
/**查看更多*/
- (void)seeMoreComments:(UIButton *)sender{
    if (self.block) {
        self.block(sender, self.myIndexPath);
    }
    
    
//    tableViewHeight += 10;
//    MyLog(@"%ld",self.myIndexPath.row);
//    if ([_delegate respondsToSelector:@selector(reloadTableViewIndexRow:)]) {
//        [_delegate reloadTableViewIndexRow:self.myIndexPath];
//    }
    
}

- (void)clickedImageView{
    if (self.clickedImageViewBlock) {
        self.clickedImageViewBlock();
    }
    
    
}

@end
